package com.windoent.yugioh.ui.activity.meinfo;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baidu.mobstat.StatService;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.windoent.yugioh.R;
import com.windoent.yugioh.adapter.brvah.MeMessageAdapter;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.bean.MyMsgBean;
import com.windoent.yugioh.http.PhoneLive;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.BaseUtils;
import com.windoent.yugioh.utils.NotificationsUtils;
import com.windoent.yugioh.utils.PermissionTool;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.widget.ActivityTitle;
import com.windoent.yugioh.widget.dialog.DeleteSureDialog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;

import java.util.List;

import okhttp3.Call;

/**
 * author : robert
 * date   : 2021/5/10 16:42
 * desc   :我的消息
 */
public class MyMsgActivity extends BaseActivity {
    private ActivityTitle mActivityTitle;
    private LinearLayout mLlServiceNotification;
    private TextView mTvOpenNotification;
    private LinearLayout mLlNoReminder;

    private LinearLayout mLlNodata;
    private RecyclerView mRecyclerView;
    private MeMessageAdapter adapter;
    private SmartRefreshLayout mSmartLayout;

    @Override
    public int getLayout() {
        return R.layout.activity_me_msg;
    }

    @Override
    public void initView() {
        super.initView();
        mActivityTitle = (ActivityTitle) findViewById(R.id.activity_title);
        immersiveStatusBar(mActivityTitle.getView());
        mLlServiceNotification = (LinearLayout) findViewById(R.id.ll_service_notification);
        mTvOpenNotification = (TextView) findViewById(R.id.tv_open_notification);
        mLlNoReminder = (LinearLayout) findViewById(R.id.ll_no_reminder);

        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        mSmartLayout = (SmartRefreshLayout) findViewById(R.id.smart_layout);
    }


    @Override
    public void initData() {
        super.initData();
        initAdapter();
        initRefreshData();
        fresh();
    }

    @Override
    public void initListener() {
        super.initListener();
        mActivityTitle.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mTvOpenNotification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //开启权限
                //推送权限打开
                NotificationPermission();
            }
        });
        mLlNoReminder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mLlServiceNotification.setVisibility(View.GONE);
            }
        });
    }

    private void initRefreshData() {
        //下拉刷新
        mSmartLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                fresh();
            }
        });
    }

    private void fresh() {
        getMeMessage();
    }

    private void initAdapter() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new MeMessageAdapter();
        mRecyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> baseQuickAdapter, @NonNull View view, int position) {
                List<MyMsgBean.DataBean.ResBean> data = adapter.getData();
                if (data != null) {
                    MyMsgBean.DataBean.ResBean bean = data.get(position);
                    if (bean != null) {
                        String name = bean.getName();
                        if (BaseMethod.notNull(name) && name.contains("系统")) {
                            startActivity(new Intent(MyMsgActivity.this, SystemNotificayionActivity.class));
                        } else {
                            startActivity(new Intent(MyMsgActivity.this, SaishiNotificayionActivity.class));
                        }
                    }
                }
            }
        });
    }

    //列表
    private void getMeMessage() {
        showLoadingDialog("");
        PhoneLive.getMeMessage(getUserToken(), new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
                mSmartLayout.finishRefresh(false);
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            MyMsgBean json = gson.fromJson(response, MyMsgBean.class);
                            if (json != null) {
                                MyMsgBean.DataBean data = json.getData();
                                if (data != null) {
                                    mSmartLayout.finishRefresh(true);
                                    List<MyMsgBean.DataBean.ResBean> res = data.getRes();
                                    if (res != null) {
                                        adapter.setNewInstance(res);
                                    }
                                } else mSmartLayout.finishRefresh(false);
                            } else mSmartLayout.finishRefresh(false);
                        } else mSmartLayout.finishRefresh(false);
                    } catch (JSONException e) {
                        mSmartLayout.finishRefresh(false);
                        e.printStackTrace();
                    }
                } else {
                    mSmartLayout.finishRefresh(false);
                    ToastUtils.showText(getString(R.string.string_response_error));
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, "我的消息界面");
        boolean y = NotificationsUtils.isNotificationEnabled(MyMsgActivity.this);//有权限
        mLlServiceNotification.setVisibility(y ? View.GONE : View.VISIBLE);
    }


    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPageEnd(this, "我的消息界面");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }

    private void NotificationPermission() {
        if (!NotificationsUtils.isNotificationEnabled(MyMsgActivity.this)) {
            //没有通知权限，跳转设置打开
            DeleteSureDialog sureDialog = new DeleteSureDialog(MyMsgActivity.this);
            sureDialog.setTvContent(getString(R.string.string_no_permission_notification));
            sureDialog.setYesOnclickListener(new DeleteSureDialog.onYesOnclickListener() {
                @Override
                public void onYesClick() {
                    PermissionTool.requestNotify(MyMsgActivity.this);
                }
            });
            sureDialog.show();
        }
    }

}
